#!/bin/bash
# ENV
script_dir='/usr/lib/zabbix/alertscripts/internal_web_monitor'

# Include declare file
. ${script_dir}/declare.txt

# Loop
for url_name in `cat ${script_dir}/curl_list.txt|grep -v '#'`
do
    url_ip=`echo $url_name|awk -F'[:/]+' '{print $2}'`
    url_port=`echo $url_name|awk -F'[:/]+' '{print $3}'`
    rspcode=`curl -m 5 -s -o /dev/null -w "%{http_code}" ${url_name}`;

    # rspbody_judg 当地址无法访问时,告警
    if [ ${rspcode} -eq 000 ];then 
        rspbody=000;
    else
        rspbody=`curl -m 5 -s $url_name`
    fi

    # problem_judg，设置临时变量，减少判断代码
    if [ $rspcode != 200 -o $rspbody != 'up' ];then
        url_problem=1
    elif [ $rspcode = 200 -a $rspbody = 'up' ];then
        url_problem=0
    fi

    # host_name_judg curl_list容错
    if [ ! ${dic_hostname[$url_ip]} ];then
        continue
        echo 'error ip'
    else
        host_name=${dic_hostname[$url_ip]}
    fi

    # 获取最后一次告警结果
    last_time_error=`grep lasttime_${host_name}_${url_port} ${script_dir}/problem_hosts.txt|awk -F'_' '{print $1}'`

    # Last Problem 
    # URL无告警，不操作
    if [ $url_problem -eq 0 -a $last_time_error = 'up' ];then
        continue
    # URL告警持续，不操作
    elif [ $url_problem -eq 1 -a $last_time_error = 'down' ];then
        continue
    # URL报错，修改最后一次的状态，第一次出错，告警
    elif [ $url_problem -eq 1 ];then
        sed -i "s%up_lasttime_${host_name}_${url_port}%down_lasttime_${host_name}_${url_port}%g" ${script_dir}/problem_hosts.txt 
        curl -H "Content-type: application/json" -X POST     -d "{                                                                           
            \"app\": \"xxxxxxxxxxxxxxxx替换AIOPS告警tokenxxxxxxxxxxxxxxx\",   
            \"eventId\": \"${host_name}:${url_port}\",
            \"eventType\": \"trigger\",
            \"alarmName\": \"${host_name}:${url_port} 内网WEB监控故障\",
            \"priority\": 2,          
            \"alarmContent\": {       
                #\"ResponseCode\": \"${rspcode}\",
                #\"ResponseBody\": \"${rspbody}\"
                \"响应代码\": \"${rspcode}\",
                \"响应内容\": \"${rspbody}\"
            }                         
            }"    "http://api.aiops.com/alert/api/event"

    # URL恢复，修改最后一次的状态,解除告警
    elif [ $url_problem -eq 0 ];then
        sed -i "s%down_lasttime_${host_name}_${url_port}%up_lasttime_${host_name}_${url_port}%g" ${script_dir}/problem_hosts.txt 
        curl -H "Content-type: application/json" -X POST     -d "{                                                                           
            \"app\": \"xxxxxxxxxxxxxxxx替换AIOPS告警tokenxxxxxxxxxxxxxxx\",   
            \"eventId\": \"${host_name}:${url_port}\",
            \"eventType\": \"resolve\",
            \"alarmName\": \"${host_name}:${url_port} 内网WEB监控恢复\",
            \"priority\": 2,          
            \"alarmContent\": {       
                #\"ResponseCode\": \"${rspcode}\",
                #\"ResponseBody\": \"${rspbody}\"
                \"响应代码\": \"${rspcode}\",
                \"响应内容\": \"${rspbody}\"
            }                         
            }"    "http://api.aiops.com/alert/api/event"
    fi
done
touch /tmp/internal_web.log
